<?php

class controller_moduleposition extends __registry_controller {

    public $_ = array();
    public $_layout = NULL;
    public $_content_header = 'OPEN';
    public $_content_top = 'OPEN';

    public function __construct() {
        parent::add_object(array(
            'handle_string',
            'handle_upload',
            'handle_paging',
            'model_moduleposition',
            'handle_validate',
			'handle_permission',
            'model_users',
            'model_categories',
            'model_language'
        ));
    }

    public function index_action($id_com = 3) {
        
		/* Check permission */
		$this->per_check_permission('index');
		
        $strHtmlLisComs = '';
        $this->_['Com_ID'] = '';

        $strHtmlok = '';
        $strHtml = $this->view->view_display('index');
        $arrHtml = $this->view->view_get_block_html($strHtml);

        $status_href = $this->_seokey[0] . '/' . $this->_seokey[1];
        $list_coms = $this->md_moduleposition_get_list_com();

        /* set ds com */
        if (!empty($list_coms)) {
            foreach ($list_coms as $key => $value) {
                $strHtmlLisComs .= $this->view->view_assign($arrHtml['com'], array(
                    'com_name' => $value['CatName'],
                    'com_id' => $value['ID'],
                    'url' => $status_href,
                    'class' => $value['ID'] == $id_com ? 'class="ui-selected"' : ''
                        ));
            }
        }
        $this->_['status_href'] = $status_href;
        $this->_['url'] = $status_href;

        $list_pos = $this->md_moduleposition_get_list_position_by_com($id_com);

        if (!empty($list_pos)) {
            foreach ($list_pos as $key => $value) {
                $value['Status'] == 'true' ? $sta = 'false' : $sta = 'true';
                $change_Status = "change_Status('" . $status_href . "','"
                        . $value['Position'] . "','"
                        . $value['ID'] . "','"
                        . $value['ComID'] . "','"
                        . $sta . "'"
                        . ")";
                $delete_module = "delete_module('" . $status_href . "','"
                        . $value['Position'] . "','"
                        . $value['ID'] . "','"
                        . $value['ComID'] . "'"
                        . ")";
                $strHtmlok .= $this->view->view_assign($arrHtml['list'], array(
                    'list_module' => $value['ModuleTitle'],
                    'list_position' => $value['Position'],
                    'order' => $value['Order'],
                    'status' => $value['Status'] == 'true' ? '{true}' : '{false}',
                    'class_status' => $value['Status'] == 'true' ? "accept" : "inactive",
                    'id_mod' => $value['Position'] . '-' . $value['ID'] . '-' . $value['ComID'],
                    'id_modu' => $value['ID'],
                    'change_Status' => $change_Status,
                    'delete_module' => $delete_module,
                    'list_id' => $value['Position'] . '/' . $value['ID'] . '/' . $value['ComID'],
                    'com_id' => $value['ComID'],
                    'href_edit_module' => URL_BASE . $status_href . '/chinh-sua/' . $value['Position'] . '/' . $value['ID'] . '/' . $value['ComID'],
                        ));
            }
        }
        $this->_['url_add'] = URL_BASE . $status_href . '/them-moi/' . $id_com;
        $this->_['url_copy'] = URL_BASE . $status_href . '/sao-chep/' . $id_com;
        $strHtmlContent = $this->view->view_assign_block($strHtml, array('list_coms' => $strHtmlLisComs
            , 'htmlnew' => $strHtmlok));
        $strHtmlContent = $this->view->view_assign($strHtmlContent, $this->_);
        return $strHtmlContent;
    }

    public function index_ajax_action() {
        
		/* Check permission */
		$this->per_check_permission('index');
		
		
		$this->_layout = 'NO_LAYOUT';

        switch ($this->_seokey[3]) {
            case "savecopy":
                $arrListData = explode(',', $this->_seokey[4]);
                foreach ($arrListData as $value) {
                    $this->md_moduleposition_CopyPos($value, $this->_seokey[5]);
                }             
                 return $this->index_action($this->_seokey[5]);
                break;
            case "loaddatacopy":
                $strDataCopy = '';
                $arrComsCopy = $this->md_moduleposition_get_list_position_by_com($this->_seokey[4]);

                $strHtml = $this->view->view_display('copy');
                $arrHtml = $this->view->view_get_block_html($strHtml);
                foreach ($arrComsCopy as $key => $value) {
                    if ($value['Status'] == 'true') {
                        $status = '{true}';
                    } else {
                        $status = '{false}';
                    };
                    $id_mod = $value['Position'] . '-' . $value['ID'] . '-' . $value['ComID'];
                    $strDataCopy.=$this->view->view_assign($arrHtml['source'], array(
                        'sr_module' => $value['ModuleTitle'],
                        'sr_position' => $value['Position'],
                        'sr_order' => $value['Order'],
                        'sr_status' => $status,
                        'id_mod' => $id_mod
                            ));
                }
                return $strDataCopy;
                break;
            case "loadposition":
                /* admin/he-thong-quan-ly-vi-tri-module/ajax/loadposition+/4 */
                //$html
                $html = $this->index_action($this->_seokey[4]);
                $a['Com_ID'] = $this->_seokey[4];
                $this->view->view_assign($html, $a);
                return $html;
                break;
            case "loaddata":
                $arrPos = array("content_header",
                    "content_left",
                    "content_top",
                    "content_mid",
                    "content_bot",
                    "content_right",
                    "content_footer");

                if (in_array($this->_seokey[4], $arrPos)) {
                    $arrlismodule = $this->md_moduleposition_get_list_module_public($this->_seokey[4], $this->_seokey[5]);
                    $list_module = '';
                    if (!empty($arrlismodule)) {
                        $list_module = '<label  class = "desc">{header_modulename}</label>';
                        $list_module .= '<select id="module" class="field text small " name="modulename" >';
                        foreach ($arrlismodule as $module) {
                            $list_module .= '<option label ="' . $module['ModuleTitle'] . '" value = "' . $module['ID'] . '" >' . $module['ModuleTitle'] . '</option>';
                        }
                        $list_module .= '</select>';
                    } else {
                        $list_module = '<label class="err">{khong_module}</label>';
                        $list_module .= '<input type="hidden" name="err_com" value="err_com"/>';
                    }
                    return $list_module;
                }
                else
                    return FALSE;
                break;
            case "change":
                /* admin/he-thong-quan-ly-vi-tri-module/ajax/change/true/content_header/1/10 */
                $status = $this->_seokey[4];
                $position = $this->_seokey[5];
                $module = $this->_seokey[6];
                $com = $this->_seokey[7];

                $rs_doi_tinh_trang = $this->md_moduleposition_update_module_position($module, $com, $position, $status);

                if ($rs_doi_tinh_trang) {
                    return $this->index_action($com);
                } else {
                    echo 'error';
                }

                break;
            case "changeall":
                /* admin/he-thong-quan-ly-vi-tri-module/ajax/changeall/true/content_top-2-2,content_top-1-2 */
                $status = $this->_seokey[4];
                $listId = $this->_seokey[5];
                $arrListId = explode(',', $listId);
                $position = '';
                $module = '';
                $com = '';
                $rs = true;
                foreach ($arrListId as $key => $value) {
                    if ($value != '') {
                        $arrId = explode('-', $value);
                        $position = $arrId[0];
                        $module = $arrId[1];
                        $com = $arrId[2];
                        $r = $this->md_moduleposition_update_module_position($module, $com, $position, $status);
                        $rs = $rs && $r;
                    }
                }
                if ($rs) {
                    return $this->index_action($com);
                } else {
                    echo 'error';
                }

                break;
            case "delete":
                /* admin/he-thong-quan-ly-vi-tri-module/ajax/delete/content_header/1/10 */
                $position = $this->_seokey[4];
                $module = $this->_seokey[5];
                $com = $this->_seokey[6];
                $rs = $this->md_moduleposition_delete_module_position($module, $com, $position);

                if ($rs) {

                    return $this->index_action($com);
                }
                break;
            case "deleteall":
                $position = '';
                $module = '';
                $com = '';
                $rs = true;
                /* admin/he-thong-quan-ly-module/ajax/delete/content_top-2-2,content_top-1-2 */
                $listId = $this->_seokey[4];
                $arrListId = explode(',', $listId);

                foreach ($arrListId as $key => $value) {
                    if ($value != '') {
                        $arrId = explode('-', $value);
                        $position = $arrId[0];
                        $module = $arrId[1];
                        $com = $arrId[2];
                        $r = $this->md_moduleposition_delete_module_position($module, $com, $position);
                        $rs = $rs && $r;
                    }
                }
                if ($rs) {

                    return $this->index_action($com);
                }
                break;
            case "order":
                /* admin/he-thong-quan-ly-vi-tri-module/ajax/order/,content_top-4-3,content_header-3-3,content_header-2-3/ */
                $listId = $this->_seokey[4];
                $arrListId = explode(',', $listId);
                $position = '';
                $module = '';
                $com = '';

                foreach ($arrListId as $key => $value) {
                    if ($value != '') {
                        $arrId = explode('-', $value);
                        $position = $arrId[0];
                        $module = $arrId[1];
                        $com = $arrId[2];
                        $this->md_moduleposition_update_order_module_position($module, $com, $position, $key);
                    }
                }
                break;
            default:
                break;
        }
    }

    public function copy_action() {
	
		/* Check permission */		
		if( !$this->per_check_permission('index',true) )
	   {
		   $this->_layout ='error';
		   $strHtmlContent = $this->_['not_permission'];
		 
	   }else{		
			$this->_layout = 'iframe';
			$ListComsCopy = '';
			$id_com = 3;
			$strHtml = $this->view->view_display('copy');
			$arrHtml = $this->view->view_get_block_html($strHtml);
			$list_coms = $this->md_moduleposition_get_list_com();

			if (!empty($list_coms)) {
				foreach ($list_coms as $key => $value) {
					$ListComsCopy .= $this->view->view_assign($arrHtml['Cat'], array(
						'CatName' => $value['CatName'],
						'ID' => $value['ID']
							));
				}
			}
			$strHtmlContent = $this->view->view_assign_block($strHtml, array('Cats' => $ListComsCopy));
			
			$strHtmlContent = $this->view->view_assign($strHtmlContent, $this->_);
		}
        return $strHtmlContent;
    }

    public function add_action() {

		/* Check permission */		
		if( !$this->per_check_permission('index',true) )
	   {
		   $this->_layout ='error';
		   $strHtml = $this->_['not_permission'];
		 
	   }else{		
			$this->_layout = 'iframe';
			
			  /*=================open Khai bao bien============================= */
			$this->_['label-error'] = '';
			/* ================= end Khai bao bien ==================== */
			/* =================save data============================= */
			$this->_['href_cancel'] = $status_href = URL_BASE . $this->_seokey[0] . '/' . $this->_seokey[1];
			if (isset($_POST) && !empty($_POST)) {
				$err = isset($_POST["err_com"]) && $_POST["err_com"] != '' ? $_POST["err_com"] : '';
				$moduleid = isset($_POST["modulename"]) && $_POST["modulename"] != '' ? $_POST["modulename"] : '';
				$position = isset($_POST["position"]) && $_POST["position"] != '' ? $_POST["position"] : '';
				$order = isset($_POST["order"]) && $_POST["order"] != '' ? $_POST["order"] : '';
				$com_id = isset($_POST["com_id"]) && $_POST["com_id"] != '' ? $_POST["com_id"] : '';
				$status = isset($_POST["status"]) ? 'true' : 'false';

				if ($err != 'err_com') {
					$result = $this->md_moduleposition_add_module_position($moduleid, $com_id, $position, $order, $status);
				} else {
					$result = false;
				}
				if ($result) {
					$href = $this->_seokey[0] . '/' . $this->_seokey[1];
					$url = URL_BASE . $href;

					$this->request->rq_goto_window_parent($url);
				}
			}
			$this->_['url'] = $this->_seokey[0] . '/' . $this->_seokey[1];
			/* ================= end save data=================================== */
			/* get thml */
			$strHtml = $this->view->view_display('add');

			$this->_['com_id'] = $this->_seokey[3];
			/* =================set data vao html============================= */
		}
        return $strHtml;
    }

}